import java.io.Serializable;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

public class User implements Serializable {
	
	private Integer id;
	private String firstName;
	private String lastName;
	private String email;
	private String middleName;
	private Set addresses = new HashSet();

	// full constructor
	public User(String firstName, String lastName, String email, String middleName) {
		this.firstName = firstName;
		this.lastName = lastName;
		this.email = email;
		this.middleName = middleName;
	}
	
	// default constructor
	public User() {
	} 
	
	public Set getAddresses() {
		return addresses;
	}

	public void setAddresses(Set addresses) {
		this.addresses = addresses;
	}
	
	public void addAddress(Address address) {
		getAddresses().add(address);
	}
	
	public Address getAddress(int index) {
	    Set addresses = getAddresses();
	    Iterator it = addresses.iterator();
	    int i = 1;
	    while (i <= addresses.size() && it.hasNext()) {
	        if (i == index){
	            return (Address) it.next();
	        } else {
	            it.next();
	            i++;
	        }
	    }
	    return null;
	}
	
	public Integer getId() {
		return this.id;
	}
	
	protected void setId(Integer id) {
		this.id = id;
	}
	
	public String getFirstName() {
		return this.firstName;
	}
	
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	
	public String getLastName() {
		return this.lastName;
	}
	
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
	public String getEmail() {
		return this.email;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}

	public String getMiddleName() {
		return middleName;
	}

	public void setMiddleName(String middleName) {
		this.middleName = middleName;
	}

	/**
	 * @see java.lang.Object#equals(Object)
	 */
	public boolean equals(Object object) {
		if (!(object instanceof User)) {
			return false;
		}
		User rhs = (User) object;
		return new EqualsBuilder().appendSuper(super.equals(object)).append(
				this.addresses, rhs.addresses).append(this.email, rhs.email)
				.append(this.middleName, rhs.middleName).append(this.firstName,
						rhs.firstName).append(this.id, rhs.id).append(
						this.lastName, rhs.lastName).isEquals();
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return new HashCodeBuilder(1085922647, -1075152501).appendSuper(
				super.hashCode()).append(this.addresses).append(this.email)
				.append(this.middleName).append(this.firstName).append(this.id)
				.append(this.lastName).toHashCode();
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return new ToStringBuilder(this).append("middleName", this.middleName)
				.append("lastName", this.lastName).append("id", this.id)
				.append("email", this.email)
				.append("firstName", this.firstName).append("addresses",
						this.addresses).toString();
	}
}
